package top.yangyitao.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import top.yangyitao.dto.PaySlipDto;
import top.yangyitao.dto.ResultDto;
import top.yangyitao.dto.TableDto;
import top.yangyitao.entity.PaySlip;
import top.yangyitao.entity.User;
import top.yangyitao.service.PaySlipService;
import top.yangyitao.service.UserService;

@RestController
public class PaySlipController {

	@Autowired
	PaySlipService paySlipService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("paySlip_html")
	@RequestMapping("/paySlip_html")
	public ModelAndView paySlip_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/paySlip.html");
		return mv;
	}
	
	@RequiresPermissions("paySlip_html")
	@RequestMapping("/addPaySlip")
	public ResultDto addPaySlip(String printDay,String startDay,String endDay,int workDays,int lateOrLeaveTimes,
			int absentTimes,String realCash,String cash,String bonusCash,String username) {
		User user = userService.findByUsername(username);
		if(user!=null) {
			PaySlip paySlip = new PaySlip();
			paySlip.setPrintDay(printDay);
			paySlip.setStartDay(startDay);
			paySlip.setEndDay(endDay);
			paySlip.setWorkDays(workDays);
			paySlip.setLateOrLeaveTimes(lateOrLeaveTimes);
			paySlip.setAbsentTimes(absentTimes);
			paySlip.setRealCash(realCash);
			paySlip.setCash(cash);
			paySlip.setBonusCash(bonusCash);
			paySlip.setUser(user);
			paySlipService.save(paySlip);
			return new ResultDto("添加工资条成功",null,0);
		}
		else {
			return new ResultDto("用户名不存在",null,2);
		}
	}

	
	@RequiresPermissions("paySlip_html")
	@RequestMapping("/getPaySlipPage")
	public TableDto<PaySlipDto> getPaySlipPage(Integer pageNumber,Integer pageSize,Long organizationId,String startDay,String endDay,String search) {
		Page<PaySlip> pageResult = paySlipService.getPaySlipPage(pageNumber,pageSize,organizationId,null,startDay,endDay,search);
		List<PaySlip> paySlips = pageResult.getContent();
		List<PaySlipDto> paySlipDtos = new ArrayList<PaySlipDto>();
		paySlips.forEach(paySlip->paySlipDtos.add(new PaySlipDto().convert(paySlip)));
		return new TableDto<PaySlipDto>(paySlipDtos,pageResult.getTotalElements());
	}
}
